Page({
  data: {
    chatData: null,
    inputMessage: "",
   
    scrollToView: ""
  },

  onLoad: function(options) {
    if (options.chatData) {
      const chatData = JSON.parse(decodeURIComponent(options.chatData));
      this.setData({
        chatData: chatData
      });
      
      // 设置导航栏标题
      wx.setNavigationBarTitle({
        title: chatData.name
      });
    }
    
    // 加载用户头像
    this.loadUserAvatar();
  },

  loadUserAvatar: function() {
    
    const userAvatar = "/images/icons/avatar.png";
    this.setData({ userAvatar });
  },

  onInput: function(e) {
    this.setData({
      inputMessage: e.detail.value
    });
  },

  sendMessage: function() {
    if (!this.data.inputMessage.trim()) return;
    
    const newMessage = {
      type: "sent",
      content: this.data.inputMessage,
      time: this.getCurrentTime()
    };
    
    const chatData = {...this.data.chatData};
    chatData.messages.push(newMessage);
    
    this.setData({
      chatData: chatData,
      inputMessage: ""
    });
    
    // 自动滚动到底部
    setTimeout(() => {
      this.setData({
        scrollToView: 'scroll-bottom'
      });
    }, 100);
  },

  getCurrentTime: function() {
    const now = new Date();
    return `${now.getHours()}:${now.getMinutes().toString().padStart(2, '0')}`;
  },

  navigateBack: function() {
    wx.navigateBack();
  },

  onAvatarError: function(e) {
    console.log('头像加载失败', e);
    // 加载默认头像
    this.setData({
      userAvatar: "/images/default-avatar.png"
    });
  }
});